package com.orangelife.mobile.main.biz;

import android.text.TextUtils;

import com.common.sp.CommSp2;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.constants.AnnouncementURL;
import com.orangelife.mobile.constants.CommonURL;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.PersonalURL;
import com.orangelife.mobile.util.LogE;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主界面的业务逻辑
 * <p/>
 * Created by 吴格非 on 2015/11/17.
 *
 * @since 3.4.1
 */
public class MainHttpBz {

    public final static String FETRUES_TYPE_1 = "1";
    public final static String FETRUES_TYPE_2 = "2";

    /** 小橙客服的电话（服务端文档显示此接口已废弃） */
    public static void getCustomerServicePhone() {
        VolleyHttpUtil.getInstance().getWithCallback(CommonURL.PHONE, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                if (OrangeErrorHandler.getInstance().isSuccess(jsonObject, false)) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(jsonObject);
                    if (map != null) {
                        String phone = String.valueOf(map.get("phone"));
                        if (!TextUtils.isEmpty(phone)) {
                            LogE.E("comm phone(from main http biz)=" + phone);
                            CommSp2.setCustomerServicePhone(phone);
                        }
                    }
                }
            }
        });
    }


    /** 获取广告列表 */
    public static void getAdList(VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("showLocation", "1");
        VolleyHttpUtil.getInstance().getWithCallback(Constant.URL_ADVERT_LIST, map, callback);
    }

    public static List<Map<String, Object>> handleAdList(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response, false)) {
            return OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);
        }
        return null;
    }

    public static void requestFeatures(VolleyHttpUtil.ResponseCallBack callback, String type) {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", type);
        VolleyHttpUtil.getInstance().getWithCallback(Constant.URL_FETRUES, map, callback);
    }

    public static List<Map<String, Object>> handleFeatures(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response, false)) {
            return OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<Map<String, Object>>>() {
            }.getType(), null, null);
        }
        return null;
    }

    /** 请求抢红包，返回红包金额 */
    public static void getRedEnvelopeMoney(VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> map = new HashMap<>();
        VolleyHttpUtil.getInstance().putWithCallback(CommonURL.URL_RED_ENVELOPE_MONEY, map, callback);
    }

    /** 请求现在到下一次抢红包所剩余的时间，返回毫秒 */
    public static void getNextRedEnvelopeTime(VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().getWithCallback(CommonURL.URL_RED_ENVELOPE_NEXT_TIME, callback);
    }

    /** 将秒数转为小时 */
    public static String convertSecondToHour(String second) {
        String result = "";
        int secondInt;
        try {
            secondInt = Integer.parseInt(second);
            int h = secondInt / 3600;
            int m = (secondInt - h * 3600) / 60;
            int s = secondInt % 60;
            result = String.format("%02d:%02d:%02d", h, m, s);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void getAllads(VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().getWithCallback(AnnouncementURL.ROLL_ADS, callback);
    }

    public static void getMyCommActOrderNum(VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().getWithCallback(PersonalURL.MY_COMM_ACT_NUM, callback);
    }

}
